﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;
using System.Windows.Forms;

namespace TouchUmlUI
{

    public enum OurToolBarItemImage { IMAGE_OFF = 0, IMAGE_ON = 1, IMAGE_CLICKED = 2 };    

    public class OurToolBarItem : Canvas
    {
        Image imageOff;
        Image imageOn;
        Image imageClicked;
        String name;
        static String[] fileTypes = {"off","on","clicked"};
        double width;
        double height;
        public OurToolBarItemImage type = OurToolBarItemImage.IMAGE_OFF;
        OurToolBar parent;

        public void loadImage(OurToolBarItemImage type, String path) {
            String file = path + name + fileTypes[(int)type] + ".png";
            System.Console.WriteLine(file);
            BitmapImage source = new BitmapImage();
            source.BeginInit();
            source.UriSource = new Uri(new Uri(Directory.GetCurrentDirectory()), file);
            source.EndInit();                        
            
            if (type == OurToolBarItemImage.IMAGE_OFF)
            {
                imageOff.Width = width;
                imageOff.Height = height;
                imageOff.Source = source;
                imageOff.Stretch = Stretch.Fill;
            }
            else if (type == OurToolBarItemImage.IMAGE_ON)
            {
                imageOn.Width = width;
                imageOn.Height = height;
                imageOn.Source = source;
                imageOn.Stretch = Stretch.Fill;
            }
            else if (type == OurToolBarItemImage.IMAGE_CLICKED)
            {
                imageClicked.Width = width;
                imageClicked.Height = height;
                imageClicked.Source = source;
                imageClicked.Stretch = Stretch.Fill;
            }
            
        }

        public void mouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (type != OurToolBarItemImage.IMAGE_CLICKED)
            {
                this.type = OurToolBarItemImage.IMAGE_ON;
                Refresh();
            }
        }

        public void mouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (type != OurToolBarItemImage.IMAGE_CLICKED)
            {
                this.type = OurToolBarItemImage.IMAGE_OFF;
                Refresh();
            }
        }

        public void mouseClick(object sender, System.Windows.Input.MouseEventArgs e)
        {
            parent.clearAll();
            this.type = OurToolBarItemImage.IMAGE_CLICKED;
            Refresh();
        }

        public OurToolBarItem(double px, double py,double width, double height,String name)  {            
            this.width = width;
            this.height = height;
            this.Width = width;
            this.Height = height;
            this.name = name;
            Canvas.SetLeft(this, px);
            Canvas.SetTop(this, py);
            imageOff = new Image();
            imageOn = new Image();
            imageClicked = new Image();
            this.MouseEnter += mouseEnter;
            this.MouseLeave += mouseLeave;
            this.MouseDown += mouseClick;
        }

        public void setParent(OurToolBar otb)
        {
            parent = otb;
        }

        public void Refresh()
        {
            System.Console.WriteLine("Refresh()!");
            this.Children.Clear();
           if (type == OurToolBarItemImage.IMAGE_OFF)
           {
               Canvas.SetLeft(imageOff, 0);
               Canvas.SetTop(imageOff, 0);
               this.Children.Add(imageOff);
           }
           else if (type == OurToolBarItemImage.IMAGE_ON)
           {
               Canvas.SetLeft(imageOn, 0);
               Canvas.SetTop(imageOn, 0);
               this.Children.Add(imageOn);
           }
            if (type == OurToolBarItemImage.IMAGE_CLICKED)
            {
                Canvas.SetLeft(imageClicked, 0);
                Canvas.SetTop(imageClicked, 0);
                this.Children.Add(imageClicked);
            }
        }

    }

    public class OurToolBar : Canvas
    {
        static String img_bg_path = System.Environment.CurrentDirectory + "\\icons\\toolbarbg.png";
        public int selected;
        OurToolBarItem itemDraw;
        OurToolBarItem itemEdit;
        OurToolBarItem itemNone;
        double ox, oy;
        double dx, dy;
        InkCanvas parent;
        Canvas canvasParent;
        bool drag = false;
        int lx, ly;
        Window1 windowParent;

        public void setCanvasParent(Canvas par)
        {
            canvasParent = par;
        }

        public void setParent(InkCanvas par)
        {
            parent = par;
        }

        public void setWindowParent(Window1 win)
        {
            windowParent = win;
        }

        public void mouseDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            lx = (int)canvasParent.Margin.Left;
            ly = (int)canvasParent.Margin.Top;
            System.Console.WriteLine(lx + " - " + ly);
            ox = (int)e.GetPosition(parent).X;
            oy = (int)e.GetPosition(parent).Y;
            this.CaptureMouse();
            drag = true;
        }

        public void mouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (drag)
            {                
                int mx = (int)e.GetPosition(parent).X;
                int my = (int)e.GetPosition(parent).Y;                
                dx = ox + mx;
                dy = oy + my;                
                InkCanvas.SetLeft(canvasParent, lx + dx);
                InkCanvas.SetTop(canvasParent, ly + dy);
            }
        }

        public void mouseUp(object sender, EventArgs e)
        {
            this.ReleaseMouseCapture();
            drag = false;
        }

        public void clearAll()
        {
            itemDraw.type = OurToolBarItemImage.IMAGE_OFF;
            itemEdit.type = OurToolBarItemImage.IMAGE_OFF;
            itemNone.type = OurToolBarItemImage.IMAGE_OFF;
            itemDraw.Refresh();
            itemEdit.Refresh();
            itemNone.Refresh();
        }

        public OurToolBar(double px, double py)
        {
            InkCanvas.SetLeft(this, px);
            InkCanvas.SetTop(this,py);
            this.Width = 62;
            this.Height = 119;
            BitmapImage source = new BitmapImage();
            source.BeginInit(); 
            source.UriSource = new Uri(new Uri(Directory.GetCurrentDirectory()), img_bg_path);
            source.EndInit(); 

            Image img = new Image();
            img.Width = 62;
            img.Height = 119;
            img.Source = source;
            img.Stretch = Stretch.Fill;
            this.Children.Add(img);

            itemDraw = new OurToolBarItem(18,17,22,22,"drawmode");
            itemDraw.loadImage(OurToolBarItemImage.IMAGE_OFF, "gfx\\");
            itemDraw.loadImage(OurToolBarItemImage.IMAGE_ON, "gfx\\");
            itemDraw.loadImage(OurToolBarItemImage.IMAGE_CLICKED, "gfx\\");
            itemEdit = new OurToolBarItem(18, 46,22,22,"drawmode");
            itemEdit.loadImage(OurToolBarItemImage.IMAGE_OFF, "gfx\\");
            itemEdit.loadImage(OurToolBarItemImage.IMAGE_ON, "gfx\\");
            itemEdit.loadImage(OurToolBarItemImage.IMAGE_CLICKED, "gfx\\");
            itemNone = new OurToolBarItem(18, 75,22,22, "drawmode");
            itemNone.loadImage(OurToolBarItemImage.IMAGE_OFF, "gfx\\");
            itemNone.loadImage(OurToolBarItemImage.IMAGE_ON, "gfx\\");
            itemNone.loadImage(OurToolBarItemImage.IMAGE_CLICKED, "gfx\\");
            itemDraw.setParent(this);
            itemEdit.setParent(this);
            itemNone.setParent(this);
            this.Children.Add(itemDraw);           
            this.Children.Add(itemEdit);
            this.Children.Add(itemNone);
            itemDraw.Refresh();
            itemEdit.Refresh();
            itemNone.Refresh();            
            this.MouseDown += mouseDown;
            this.MouseUp += mouseUp;
            this.MouseMove += mouseMove;
        }
    }
}
